// 注册页面的 JS 文件
Page({
  data: {
    username:'',
    phone: '',
    password: '',
    confirmPassword: '',
    realName: '',
    captcha: '',
    checked: false, // 用于记录是否勾选了协议
    gender:'male',
    nickname:'',
    address:'',
    
  },
  onGenderChange: function (e) {
    this.setData({
        gender: e.detail.value
    });
},
bindUsernameInput:function(e){
    this.setData({
        username:e.detail.value
    })
},
  bindPhoneInput: function(e) {
    this.setData({
      phone: e.detail.value
    });
  },
  bindPasswordInput: function(e) {
    this.setData({
      password: e.detail.value
    });
  },
  bindConfirmPasswordInput: function(e) {
    this.setData({
      confirmPassword: e.detail.value
    });
  },
  bindRealNameInput: function(e) {
    this.setData({
      realName: e.detail.value
    });
  },
  bindCaptchaInput: function(e) {
    this.setData({
      captcha: e.detail.value
    });
  },
  bindNicknameInput:function(e){
      this.setData({
        nickname:e.detail.value
      })
  },
  bindAddressInput:function(e){
      this.setData({
          address:e.detail.value
      })
  },
 
  sendCaptcha: function() {
    // 发送验证码的逻辑
  },

  bindMobile: function() {
      const user={
          username:this.data.username,
          phoneNumber: this.data.phone,
          password: this.data.password,
            nickname: this.data.nickname,
            gender: this.data.gender,
            realName:this.data.realName,
            address:this.data.address
            
      }
    wx.request({
      url: 'http://localhost:9090/addmembers',
      method: 'POST',
      data: user,
      success:(res)=>{
        console.log(user)
        wx.showToast({
            title: '注册成功',
            icon: 'success'
        });
      },
      fail:(err)=>{
          console.log(err)
      }
    })
  },
  onCheckboxChange: function(e) {
    // 确保 e.detail.value 是一个数组，并且包含 'agree' 字符串
    if (Array.isArray(e.detail.value) && e.detail.value.includes('agree')) {
      this.setData({
        checked: true
      });
    } else {
      this.setData({
        checked: false
      });
    }
  },
  onRegister: function() {
    // 注册逻辑
    if (!this.data.phone) {
      wx.showToast({
        title: '请输入手机号',
        icon: 'none'
      });
      return;
    }
    if (!this.data.password || this.data.password.length < 6 || this.data.password.length > 16) {
      wx.showToast({
        title: '密码长度必须在6-16位之间',
        icon: 'none'
      });
      return;
    }
    if (this.data.password !== this.data.confirmPassword) {
      wx.showToast({
        title: '两次输入的密码不一致',
        icon: 'none'
      });
      return;
    }
    if (!this.data.realName) {
      wx.showToast({
        title: '请输入真实姓名',
        icon: 'none'
      });
      return;
    }
    if (!this.data.captcha) {
      wx.showToast({
        title: '请输入验证码',
        icon: 'none'
      });
      return;
    }
    if (!this.data.checked) {
      wx.showToast({
        title: '请同意注册协议',
        icon: 'none'
      });
      return;
    }
    // 这里添加发送注册请求的代码
  }
});